package org.lewis.man.controllers;

import java.util.HashMap;
import java.util.Map;

import org.lewis.common.domain.User;
import org.lewis.common.domain.VCode;
import org.lewis.common.service.UserService;
import org.lewis.common.service.VCodeService;

import com.tocersoft.framework.core.annotations.Controller;
import com.tocersoft.framework.core.annotations.Injection;
import com.tocersoft.framework.web.annotations.Mapping;
import com.tocersoft.framework.web.annotations.NameSpace;
import com.tocersoft.framework.web.renderer.Renderer;
import com.tocersoft.framework.web.renderer.TemplateRenderer;

/**
 * 邮箱激活
 * 
 * @author taiqichao
 * 
 */
@Controller
@NameSpace("/accounts")
public class AccountActivateController {

	private final static String VIEWBASEPATH = "/WEB-INF/views/accounts";

	@Injection("userService")
	private UserService userService;
	
	@Injection
	private VCodeService vcodeService;

	@Mapping("/activate/$1")
	public Renderer activate(String code) throws Exception {
		Map<String, Object> model = new HashMap<String, Object>();
		VCode vCode=vcodeService.getByCode(code);
		if (vCode == null) {
			return new TemplateRenderer(VIEWBASEPATH + "/activate_failed.vm",model);
		} else {
			User user = userService.getUserById(vCode.getUid());
			if (user.getStatus()!=1) {
				// 更新状态
				userService.updateUserStatus(user.getId(), 1);
				//删除激活码
				vcodeService.removeCode(vCode.getId());
				return new TemplateRenderer(VIEWBASEPATH+ "/activate_success.vm", model);
			} else {
				return new TemplateRenderer(VIEWBASEPATH+ "/activate_failed.vm", model);
			}
		}
	}

}
